<?php

require_once('id3/getid3.php');
$getID3 = new getID3;

$module_dir = 'modules/mediaplayer/';
$dir = @$_GET['odir'];
if ($dir == "") {
	'/media/music/';
}
$path = '';

$dirArray = array();
$dirArrayAll = array();
$dirArrayDir = array();

if (!empty($_GET['dir'])) {
	$path = substr(urldecode($_GET['dir']), 1, (strlen(urldecode($_GET['dir']))-1));
	if (substr(urldecode($path), strlen(urldecode($path))-1, 1) !== "/") {
		$path .= "/";
	}
	$dir .= $path;
} else {
}

$prevPath = "/";
if ($path !== "") {
	$prevPathTok = explode("/", $path);
	for ($z = 0; $z < count($prevPathTok)-2; $z++) {
		if ($prevPathTok[$z] !== "") {
			$prevPath .= stripslashes($prevPathTok[$z])."/";
		}
	}
}

if (@$_GET['refresh'] !== "1") {
	echo "<div id='mediaTitleDiv' class='mediaTitle'></div>";
	echo "<div id='mediaParentDiv' class='mediaParent'></div>";
	echo "<div id='mediaBrowserDiv' class='mediaBrowser'>";
}

if (@$_GET['firstrun'] !== "1") {

	echo "<div id='parentDirDiv' class='parentDir'>".$prevPath."</div>";
	echo $path."<br>";
	?><div id='cc' class='dragdealer'>
		<ul class='wave' id='mppl'>
			
			<?php
			
			$myDirectory=opendir(trim($dir));
			while($entryName=readdir($myDirectory)) {
				if (is_dir($dir.$entryName)) {
					$dirArrayDir[]=$entryName;
				} else {
					$dirArray[]=$entryName;
				}
			}
			closedir($myDirectory);

			// Sorts files
			sort($dirArrayDir);
			sort($dirArray);

			$dirArrayAll = array_merge($dirArrayDir, $dirArray);

			// Counts elements in array
			$indexCount=count($dirArrayAll);

			$plID = -1;
			for($index=0; $index < $indexCount; $index++) {
			  // Gets File Names
			  $name=$dirArrayAll[$index];
			  $namehref=$dirArrayAll[$index];
			  $cont = 1;  

			  $id3 = $getID3->analyze($dir.$name);
			  getid3_lib::CopyTagsToComments($id3);
			  $id3_title = addslashes(@$id3['comments_html']['title'][0]);
			  $id3_artist = addslashes(@$id3['comments_html']['artist'][0]);
			  $id3_album = addslashes(@$id3['comments_html']['album'][0]);
			  $id3_genre = addslashes(@$id3['comments_html']['genre'][0]);
			  if (($id3_title == "") && (substr($name, (strlen($name)-3), 3) == "mp3")) $id3_title = @$id3['filename'];

			  if ($id3_title == "" && !is_dir($dir.$name)) continue;

			  if ($cont > 0) {
				  if($_SERVER['QUERY_STRING']=="hidden")
					  {$hide="";
					  $ahref="./";
					  $atext="Hide";}
				  else
					  {$hide=".";
					  $ahref="./?hidden";
					  $atext="Show";}
				  if(substr("$dirArrayAll[$index]", 0, 1) != $hide) {
					  $plID += 1;

					  if (is_dir($dir.$name)) {
						echo "<li id=\"".addslashes("|".$path.$name)."\" ";	
					  } else {
						echo "<li data-plid='".$plID."' data-id3title='".$id3_title."' data-id3artist='".$id3_artist."' data-id3album='".$id3_artist."' data-id3genre='".$id3_genre."' id=\"".addslashes("http://localhost/playmedia/".$path.$name)."\" ";	
					  }
						if (substr($dir, 0, 1) == "/") {
						  if (is_dir($dir.$name."/")) {
							  if (file_exists($dir."/folder.jpg")) {
								  echo "data-id3art='".$module_dir."cover-dir.php?f=".$dir.$name."/folder.jpg'>";
								  echo "<img src='".$module_dir."cover-dir.php?f=".$dir.$name."/folder.jpg'>";
							  } elseif (file_exists($dir.$name."/Album.jpg")) {
								  echo "data-id3art='".$module_dir."cover-dir.php?f=".$dir.$name."/Album.jpg'>";
								  echo "<img src='".$module_dir."cover-dir.php?f=".urlencode($dir.$name)."/Album.jpg'>";
							  } elseif (file_exists($dir.$name."/AlbumArtSmall.jpg")) {
								  echo "data-id3art='".$module_dir."cover-dir.php?f=".$dir.$name."/AlbumArtSmall.jpg'>";
								  echo "<img src='".$module_dir."cover-dir.php?f=".urlencode($dir.$name)."/AlbumArtSmall.jpg'>";
							  } elseif (file_exists($dir.$name."/Cover.jpg")) {
								  echo "data-id3art='".$module_dir."cover-dir.php?f=".$dir.$name."/Cover.jpg'>";
								  echo "<img src='".$module_dir."cover-dir.php?f=".urlencode($dir.$name)."/Cover.jpg'>";
							  } elseif (file_exists($dir.$name."/front.jpg")) {
								  echo "data-id3art='".$module_dir."cover-dir.php?f=".$dir.$name."/front.jpg'>";
								  echo "<img src='".$module_dir."cover-dir.php?f=".urlencode($dir.$name)."/front.jpg'>";
							  } elseif (file_exists($dir.$name."/frontcover.jpg")) {
								  echo "data-id3art='".$module_dir."cover-dir.php?f=".$dir.$name."/frontcover.jpg'>";
								  echo "<img src='".$module_dir."cover-dir.php?f=".urlencode($dir.$name)."/frontcover.jpg'>";
							  } elseif (file_exists($dir.$name."/Cover.jpg")) {
								  echo "data-id3art='".$module_dir."cover-dir.php?f=".$dir.$name."/Cover.jpg'>";
								  echo "<img src='".$module_dir."cover-dir.php?f=".urlencode($dir.$name)."/Cover.jpg'>";
							  } elseif (file_exists($dir.$name."/Artwork.jpg")) {
								  echo "data-id3art='".$module_dir."cover-dir.php?f=".$dir.$name."/Artwork.jpg'>";
								  echo "<img src='".$module_dir."cover-dir.php?f=".urlencode($dir.$name)."/Artwork.jpg'>";
							  } elseif (file_exists($dir.$name."/Folder.jpg")) {
								  echo "data-id3art='".$module_dir."cover-dir.php?f=".$dir.$name."/Folder.jpg'>";
								  echo "<img src='".$module_dir."cover-dir.php?f=".urlencode($dir.$name)."/Folder.jpg'>";
							  } else {
								  $o = 0;
								  $handle = opendir($dir.$name);
								  while (($file = readdir($handle)) !== false) {
									  if (substr($file, strlen($file)-3, 3) == "jpg") {
										  $o = 1;
										echo "data-id3art='".$module_dir."cover-dir.php?f=".urlencode($dir.$name."/".$file)."'>";
										echo "<img src='".$module_dir."cover-dir.php?f=".urlencode($dir.$name."/".$file)."'>";
										break;
									  }
								  }
								  if ($o == 0) {
									  $a = glob(addslashes($dir.$name."/*.mp3"), GLOB_BRACE);
									  if (count($a) > 0) {
 										echo "data-id3art='images/Music-folder-icon.png'>";
										  echo "<img src='images/Music-folder-icon.png'>";
									  } else {
										echo "data-id3art='images/Folder-icon.png'>";
										  echo "<img src='images/Folder-icon.png'>";
									  }
								  }
							  }
							  // if directory
							  $name = stripslashes(strip_tags(str_replace("TORRENTLOUNGE", "", $name)));
							  echo " <p>$name</p></li>";
						  } else {
							  // if not directory\
								if (file_exists($dir."/Folder.jpg")) {
									echo "data-id3art='".$module_dir."cover-dir.php?f=".urlencode($dir)."/Folder.jpg'>";
									echo "<img src='".$module_dir."cover-dir.php?f=".urlencode($dir)."/Folder.jpg'>";
								} else {
									echo "data-id3art='images/Music-icon.png'>";
									echo "<img src='images/Music-icon.png'>";
								}
								echo " <p>".$id3_title."</p></li>";
						  }
					  } else {
						  // if not in /media
							echo " <p>".$id3_title."</p></li>";
					  }
					}
				}
			}
			echo "</ul></div>";
} // end if not $firstrun

if (@$_GET['refresh'] !== "1") {
?>
	</div>

	<div id='fsplayer' class='fsplayer'>
		<div id='fsTitle' class='fsTitle'></div>
		<div id='fsTitle2' class='fsTitle2'></div>
		<div id='duration' style='position: absolute; top: 240px; right: 5%;'></div>
		
		<div style='margin: 5%;' width='100%'>
			<div id='mpSliderTime'></div>
		</div>
		
		<div style='height: 60%;'>
			<div id='fscontrols' class='fsControls'>
					<div id='fsPrev' style="float: left; padding: 10px;"><img src='modules/mediaplayer/images/prev.png'></div>
					<div id='fsStop' style="float: left; padding: 10px;"><img src='modules/mediaplayer/images/stop.png'></div>
					<div id='fsNext' style="float: left; padding: 10px;"><img src='modules/mediaplayer/images/next.png'></div>
			</div>		
			<div style='width:100%;'>
				<div id='mpSliderVolume2'></div>
			</div>
		</div>

		<div id='fsCoverArt' class='fsCoverArt'></div>
		<div id='fsBack' class='fsBack'><i class='fa fa-arrow-circle-o-left fa-2x'></i></div>
	</div>
	
<?php
}
?>

</div>
